<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>防抖</title>
</head>
<body>
    <div>
        <!-- keyup 模拟多次输入 -->
        <input type="text" onkeyup="inputchange()">请输入

    </div>
</body>
</html>

<script>
    function inputchange(){
        let i = document.querySelector('input')
        console.log('keyup ->',i.value);

        const ref = debounce(console.log , 500)
        ref('run deb')
    }

    // 注意timer位置
    let timer = null

function debounce( func , delay ) {
    // let timer = null   // 此处不生效！！！

	return function (...args){
        if(timer) clearTimeout(timer)
        timer = setTimeout(() => {
            func.apply(this,args)
        } , delay)
  }
}
</script>